<?php 
class ProductController extends Controller {
	var $name = "Product";
	var $uses = array();
	//var $components = array('Utils');
	
	function index() {
		$cats = $this->Utils->getCategoryList();
		
		$catArray = array();
		foreach($cats as $cat) {
			$catArray[] = $cat['Category']['id'];
		}
		
		$this->set('cats', $cats);
		$index = rand(0, count($catArray) - 1);
		$catId = $catArray[$index];
		$slug = $cats[$index]['Category']['slug'];
		$this->set('slug', $slug);
		$catId = 1;
		$new = $this->Utils->getNewArrivalsProduct($catId);
		$this->set('new', $new);
		
		$hot = $this->Utils->getHotNewRelease($catId);
		$this->set('hot', $hot);
		
		$best = $this->Utils->getBestSeller($catId);
		$this->set('best', $best);
		
		$recommend = $this->Utils->getRecommendProduct($catId);
		$this->set('recommend', $recommend);
		
		$toprate = $this->Utils->getTopRateProduct($catId);
		$this->set('toprate', $toprate);
	}
	
	function buy($asin=null) {
		$fields = array('Product.link');
		$conditions = array('Product.asin' => $asin);
		$product = $this->Product->find('first', array('fields' => $fields, 'conditions' => $conditions));
		
		if(!empty($product)) {
			$this->redirect($product['Product']['link']);
		} else {
			$this->redirect(array('controller' => 'product', 'action' => 'index'));
		}
	}
	
	function exportLink() {
		$file = ROOT . DS . APP_DIR . DS . 'webroot' . DS . 'files' . DS . 'sitemap.xml';
		$xmlData = @file_get_contents($file);
		App::import('Xml');

		// now parse it
		$parsed_xml =& new XML($xmlData);
		$parsed_xml = Set::reverse($parsed_xml); // this is what i call magic
		
		foreach($parsed_xml['Urlset']['Url'] as $url) {
			echo $url['loc'] . '</br>';
		}die;
	}
}
?>